package com.lxy.skill.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author lxy
 * @Date 2021/12/19 0:54
 * @Description
 */

@Component
public class CuratorUtil {

    @Value("${zookeeper.address}")
    private String connectString;
    /**
     * 创建节点
     * @param path
     * @param value
     * @throws Exception
     */
    public void createNode(String path,Object value) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        curatorFramework.start();

        byte[] v = JSONObject.toJSONString(value).getBytes();
        curatorFramework.create().
                creatingParentsIfNeeded().//递归创建,如果没有父节点,自动创建父节点
                withMode(CreateMode.PERSISTENT).//设置节点类型
                withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).
                forPath(path, v);
        curatorFramework.close();
    }

    /**
     * 获取节点
     * @param path
     * @return
     * @throws Exception
     */
    public Object getNode(String path) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        curatorFramework.start();

        byte[] data = curatorFramework.
                getData().
                forPath(path);

        curatorFramework.getData().usingWatcher(new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("watch机制触发" + watchedEvent);
            }
        });
        curatorFramework.close();
        return new String(data);
    }


    /**
     * 设置结点
     * @param path
     * @param value
     * @throws Exception
     */
    public void SetNode(String path,Object value) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        curatorFramework.start();
        byte[] v = JSONObject.toJSONString(value).getBytes();
        curatorFramework.setData().
                withVersion(-1).
                forPath(path, v);
        curatorFramework.close();
    }


    /**
     * 删除结点
     * @param path
     * @throws Exception
     */
    public void deleteNode(String path) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        curatorFramework.start();

        curatorFramework.delete().deletingChildrenIfNeeded().forPath(path);
        curatorFramework.close();
    }


    public CuratorFramework lock(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        client.start();
        return client;
    }
}
